package lx.scrmXj.pojo;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * @Description:
 * @Author: 李星 lixing_java@163.com
 * @Date: 2020/3/8 19:03
 */
public class ParentId {

    public static Record getDepartmentInfo(int id) {
        String sql = "select ID,PARENTID,NAME from XJ_SCRM_DEPARTMENT WHERE ID = ?";
        return Db.findFirst(sql,id);
    }

    /*
     * @Description 递归获取员工的绝对部门信息（编号和名称）
     * @Author 李星
     * @Date 2020/3/8 19:04
     * @Param [departmentId 当前部门编号]
     * @return java.lang.String 数据格式：编号|名称;编号|名称;编号|名称;
     */
    public static String getParentDepartmentInfo(int departmentId) {
        String t = "";
        Record d = getDepartmentInfo(departmentId); // TODO  根据部门编号获取部门信息
        int id = d.getInt("ID");
        String name = d.getStr("NAME");
        int parentId = d.getInt("PARENTID");
        t = id + "|" + name + ";" + t;

        if (parentId != 0) {
            Record d2 =  getDepartmentInfo(parentId); // TODO 获取部门的上 1 级部门信息
            id = d2.getInt("ID");
            name = d2.getStr("NAME");
            parentId = d2.getInt("PARENTID");
            t = id + "|" + name + ";" + t;

            if (parentId != 0) {
                Record d3 = getDepartmentInfo(parentId); // TODO 获取部门的上 2 级部门信息
                id = d3.getInt("ID");
                name = d3.getStr("NAME");
                parentId = d3.getInt("PARENTID");
                t = id + "|" + name + ";" + t;

                if (parentId != 0) {
                    Record d4 = getDepartmentInfo(parentId); // TODO 获取部门的上 3 级部门信息
                    id = d4.getInt("ID");
                    name = d4.getStr("NAME");
                    parentId = d4.getInt("PARENTID");
                    t = id + "|" + name + ";" + t;

                    if (parentId != 0) {
                        Record d5 = getDepartmentInfo(parentId); // TODO 获取部门的上 4 级部门信息
                        id = d5.getInt("ID");
                        name = d5.getStr("NAME");
                        parentId = d5.getInt("PARENTID");
                        t = id + "|" + name + ";" + t;

                        if (parentId != 0) {
                            Record d6 = getDepartmentInfo(parentId); // TODO 获取部门的上 5 级部门信息
                            id = d6.getInt("ID");
                            name = d6.getStr("NAME");
                            parentId = d6.getInt("PARENTID");
                            t = id + "|" + name + ";" + t;

                            if (parentId != 0 ) {
                                Record d7 = getDepartmentInfo(parentId); // TODO 获取部门的上 6 级部门信息
                                id = d7.getInt("ID");
                                name = d7.getStr("NAME");
                                parentId = d7.getInt("PARENTID");
                                t = id + "|" + name + ";" + t;

                                if (parentId != 0) {
                                    Record d8 = getDepartmentInfo(parentId); // TODO 获取部门的上 7 级部门信息
                                    id = d8.getInt("ID");
                                    name = d8.getStr("NAME");
                                    parentId = d8.getInt("PARENTID");
                                    t = id + "|" + name + ";" + t;

                                    if (parentId != 0) {
                                        Record d9 = getDepartmentInfo(parentId); // TODO 获取部门的上 8 级部门信息
                                        id = d9.getInt("ID");
                                        name = d9.getStr("NAME");
                                        parentId = d9.getInt("PARENTID");
                                        t = id + "|" + name + ";" + t;

                                        if (parentId != 0) {
                                            Record d10 = getDepartmentInfo(parentId); // TODO 获取部门的上 9 级部门信息
                                            id = d10.getInt("ID");
                                            name = d10.getStr("NAME");
                                            parentId = d10.getInt("PARENTID");
                                            t = id + "|" + name + ";" + t;

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return t;
    }
}
